package cn.itcast.controller;

import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    public UserService userService = null;
    public void init() throws ServletException {
        userService = new UserServiceImpl();
    }


    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取uri
        String uri = req.getRequestURI();
        //System.out.println(uri); //       类似这个格式/test/user/delete
        String pathName = uri.substring(uri.lastIndexOf("/") + 1);
       // System.out.printf(pathName);  打印的是 delete
        //System.out.println(this);//this代表继承父类的对象 这里指Person类

        //反射执行
        try {
            //获取子类中所有的方法
            Method method = this.getClass().getDeclaredMethod(pathName, HttpServletRequest.class, HttpServletResponse.class);
            //暴力反射
            method.setAccessible(true);
            //执行方法
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
